package cn.service.impl;

import cn.entity.SysArea;
import cn.listener.SysAreaListener;
import cn.mapper.SysAreaMapper;
import cn.service.SysAreaSerevice;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional//开启事务

public class SysAreaServiceImpl extends BaseServiceImpl<SysArea> implements SysAreaSerevice {
    @Autowired
    SysAreaMapper sysAreaMapper;


    @Override
    public SysArea selectArea(HashMap<String, Object> map) {
        return sysAreaMapper.selectAreas(map).get(0);
    }

    @Override
    public PageInfo<SysArea> selectPage(int pageNum, int pageSize, HashMap<String, Object> map) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<SysArea>(sysAreaMapper.selectAreas(map),4);
    }

    @Override
    public List<SysArea> selectChangeTree(Long id) {
        return sysAreaMapper.selectChangeTree(id);
    }

    @Override
    public String selectParentIds(String newId) {
        return sysAreaMapper.selectParentIds(newId);
    }

    @Override
    public int updateParentIds(SysArea areaParents) {
        return sysAreaMapper.updateParentIds(areaParents);
    }

    @Override
    public void upload(InputStream is) {
        EasyExcel.read(is,SysArea.class,new SysAreaListener(sysAreaMapper)).sheet().doRead();
    }

    @Override
    public void download(ServletOutputStream ops) {
        EasyExcel.write(ops,SysArea.class).sheet().doWrite(sysAreaMapper.selectAll());
    }

}
